package com.mdc.mansion.core.modules.basedata.impl;

import com.google.common.collect.Lists;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.basedata.RestaurantStaffReceiveDishesService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.order.dto.RestaurantStaffReceiveDishesDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.basedata.dao.RestaurantStaffReceiveDishesDao;
import com.mdc.mansion.dataaccess.modules.basedata.entity.RestaurantStaffReceiveDishes;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 菜品接收设置ServiceImpl
 * @author mingdongc
 * @version 2018-01-22
 */
@Service
public class RestaurantStaffReceiveDishesServiceImpl extends CrudService<RestaurantStaffReceiveDishesDao, RestaurantStaffReceiveDishes> implements RestaurantStaffReceiveDishesService ,ApplicationListener<ContextRefreshedEvent> {

    private RestaurantDishesService restaurantDishesService;

    @Override
    @Transactional
    public void changeCurrentStaffReceiveDishes(List<String> restaurantDishesIds) {
        List<RestaurantStaffReceiveDishes> list = findCurrentStaffReceiveDishes();
        if(CollectionUtils.isNotEmpty(list)){
            for(RestaurantStaffReceiveDishes receiveDishes:list){
                this.delete(receiveDishes);
            }
        }
        for(String dishesId:restaurantDishesIds){
            RestaurantStaffReceiveDishes staffReceiveDishes=new RestaurantStaffReceiveDishes();
            staffReceiveDishes.setRestaurantId(ContextHolder.currentRestaurant().getId());
            staffReceiveDishes.setRestaurantStaffId(ContextHolder.currentRestaurantStaff().getId());
            staffReceiveDishes.setRestaurantDishesId(dishesId);
            staffReceiveDishes.setCreateBy(RestaurantSupport.getCurrentOperationUser());
            staffReceiveDishes.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
            this.save(staffReceiveDishes);
        }
    }

    private List<RestaurantStaffReceiveDishes> findCurrentStaffReceiveDishes() {
        RestaurantStaffReceiveDishes restaurantStaffReceiveDishes=new RestaurantStaffReceiveDishes();
        restaurantStaffReceiveDishes.setRestaurantId(ContextHolder.currentRestaurant().getId());
        restaurantStaffReceiveDishes.setRestaurantStaffId(ContextHolder.currentRestaurantStaff().getId());
        return this.findList(restaurantStaffReceiveDishes);
    }

    @Override
    public List<RestaurantStaffReceiveDishesDTO> getCurrentStaffReceiveDishes() {
        List<RestaurantStaffReceiveDishesDTO> restaurantStaffReceiveDishesDTOs= Lists.newArrayList();
        List<RestaurantDishes> restaurantDishesList=restaurantDishesService.findCurrentRestaurantDishes();
        List<RestaurantStaffReceiveDishes> currentStaffReceiveDishes = this.findCurrentStaffReceiveDishes();
        for(RestaurantDishes restaurantDishes:restaurantDishesList){
            RestaurantStaffReceiveDishesDTO dishesDTO=new RestaurantStaffReceiveDishesDTO();
            BeanUtils.copyProperties(restaurantDishes,dishesDTO);
            dishesDTO.setId("");
            dishesDTO.setReceiveFlag(0);
            dishesDTO.setRestaurantDishesId(restaurantDishes.getId());
            if(CollectionUtils.isNotEmpty(currentStaffReceiveDishes)){
                for(RestaurantStaffReceiveDishes restaurantStaffReceiveDishes:currentStaffReceiveDishes){
                    if(StringUtils.equals(restaurantStaffReceiveDishes.getRestaurantDishesId(),dishesDTO.getRestaurantDishesId())){
                        dishesDTO.setReceiveFlag(1);
                    }
                }
            }
            restaurantStaffReceiveDishesDTOs.add(dishesDTO);
        }
        return restaurantStaffReceiveDishesDTOs;
    }

	@Override
	public void initializeStaffReceiveDishes(Map<String, String> paraMap) {
		this.dao.initializeStaffReceiveDishes(paraMap);
	}

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext=event.getApplicationContext();
        if (applicationContext.getParent() == null) {
            restaurantDishesService = applicationContext.getBean(RestaurantDishesService.class);
        }
    }
}
